package cn.azy.document.tool;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.context.annotation.Scope;

import java.util.List;
import java.util.Map;

/**
 * @ Author     ：AZY.
 * @ Date       ：Created in 16:09 2019/10/24
 * @ Description：${DESCRIPTION}
 */
@Slf4j
@Scope("prototype")
public class PutDataToFileAll implements Runnable {
    private List<Map<String,String>> list;
    private int j;
    private int size;
    private Sheet sheet;

    public PutDataToFileAll(List<Map<String,String>>  list, int j,int size, Sheet sheet) {
        super();
        this.list = list;
        this.j = j;
        this.size = size;
        this.sheet = sheet;
    }



    @Override
    public void run() {

        try {

            /**
             * 往Excel中写新数据
             */
            log.info((j-1)*size+"至"+j*size);
            //处理(j-1)*size+"至"+j*size的数据
            for(int i=(j-1)*size;i<=j*size && i<list.size();i++){
                // 创建一行：从第二行开始，跳过属性列
                Row row = getRow(sheet, i+1);
                // 得到要插入的每一条记录
                Map<String,String> map = list.get(i);
                // 在一行内循环
                //加锁防止每列数据错误
                synchronized (sheet){
                    String name = map.get("name");
                    Cell first = row.createCell(0);
                    first.setCellValue(name);

                    String st = map.get("st");
                    Cell second = row.createCell(1);
                    second.setCellValue(st);

                    String et = map.get("et");
                    Cell third = row.createCell(2);
                    third.setCellValue(et);

                    String drp = map.get("drp");
                    Cell fourth = row.createCell(3);
                    fourth.setCellValue(drp);
                    if(!name.equals(first.getStringCellValue())|| !st.equals(second.getStringCellValue())||
                    !et.equals(third.getStringCellValue())|| !drp.equals(fourth.getStringCellValue())){
                        log.info("name1:"+name+"   st1:"+st +"  et1:"+et+"  drp1:"+drp
                                +"   name2:"+first.getStringCellValue()+"   st2:"+second.getStringCellValue()
                                +"  et2:"+third.getStringCellValue()+"  drp2:"+fourth.getStringCellValue());
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static synchronized Row getRow(Sheet sheet, int rownum) {
        return sheet.createRow(rownum);
    }
}
